package com.ruoyi.web.timer;

import com.ruoyi.web.entity.Coupon;
import com.ruoyi.web.service.ICouponService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class Timer {
    private final ICouponService couponServiceImpl;

    public Timer(ICouponService couponServiceImpl) {
        this.couponServiceImpl = couponServiceImpl;
    }

//    @Scheduled(fixedDelay = 10000)
    @Scheduled(cron = "0 0 1 * * ?")
    public void updateCouponStatus() {
        // 创建当前时间的实例
        LocalDateTime now = LocalDateTime.now();
        // 使用MyBatis-Plus的LambdaUpdateChainWrapper来构建和执行update语句
        couponServiceImpl.lambdaUpdate()
                .lt(Coupon::getValidityPeriod, now) // 检查validity_period是否小于当前时间
                .set(Coupon::getStatus, 1)         // 设置status为1
                .update();                         // 执行update操作

    }
}
